import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../src/generated/models/article.pb.dart';
import '../../../tools/date_format.dart';
import '../../../tools/build.dart';
import '../../../routes.dart';
import '../../../responsive.dart';
import '../../../constants.dart';

class ArticleItemCard extends StatelessWidget {
  final ArticleItem blog;

  const ArticleItemCard({
    Key? key,
    required this.blog,
  }) : super(key: key);

  static List<Widget> generateArticleItemCard(List<ArticleItem>? articles) {
    if (articles == null) {
      return [Text("articles is empty")];
    }
    return List.generate(
        articles.length, (index) => ArticleItemCard(blog: articles[index]));
  }

  Widget getCardLeft(BuildContext context) {
    return Expanded(
        flex: 2,
        child: Container(
            constraints: BoxConstraints(maxWidth: 200, maxHeight: 200),
            child: BuilderTool.networkImage(blog.image)));
  }

  Widget getTitle(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: kDefaultPadding),
      child: Text(
        blog.title,
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: Responsive.isDesktop(context) ? 24 : 16,
          color: kDarkBlackColor,
          height: 1.3,
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }

  Widget getAuthor() {
    return Text(
      blog.author,
      style: TextStyle(
        color: kDarkBlackColor,
        fontSize: 12,
        fontWeight: FontWeight.bold,
      ),
    );
  }

  List<Widget> getButtons() {
    return [
      // IconButton(
      //   icon: SvgPicture.asset("assets/icons/feather_thumbs-up.svg"),
      //   onPressed: () {},
      // ),
      // IconButton(
      //   icon: SvgPicture.asset("assets/icons/feather_message-square.svg"),
      //   onPressed: () {},
      // ),
      // IconButton(
      //   icon: SvgPicture.asset("assets/icons/feather_share-2.svg"),
      //   onPressed: () {},
      // ),
    ];
  }

  Widget getCardRight(BuildContext context) {
    return Expanded(
      flex: 5,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          getTitle(context),
          Text(
            blog.description,
            maxLines: 4,
            style: TextStyle(height: 1.5),
          ),
          SizedBox(height: kDefaultPadding),
          Row(children: [
            getAuthor(),
            SizedBox(width: kDefaultPadding),
            Text(
              DateFormatTool.datetime(blog.createdAt.toInt()),
              style: Theme.of(context).textTheme.caption,
            ),
            Spacer(),
            if (Responsive.isDesktop(context)) ...getButtons(),
            TextButton(
              onPressed: () {},
              child: Container(
                padding: EdgeInsets.only(bottom: kDefaultPadding / 4),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(color: kPrimaryColor, width: 3),
                  ),
                ),
                child: InkWell(
                    onTap: () => Get.toNamed(
                        Routes.detailRoute(blog.articleId.toString())),
                    child: Text(
                      "查看更多",
                      style: TextStyle(color: kDarkBlackColor),
                    )),
              ),
            ),
          ]),
          if (!Responsive.isDesktop(context)) Row(children: [...getButtons()]),
        ],
      ),
    );
  }

  Widget getContent(BuildContext context) {
    return Padding(
        padding: const EdgeInsets.only(bottom: kDefaultPadding),
        child: Column(
          children: [
            Container(
                padding: EdgeInsets.all(kDefaultPadding),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(3)),
                ),
                child: Row(
                  children: [
                    getCardLeft(context),
                    SizedBox(width: kDefaultPadding),
                    getCardRight(context),
                  ],
                ))
          ],
        ));
  }

  @override
  Widget build(BuildContext context) {
    return getContent(context);
  }
}
